package com.xiaowen.demo;

import cn.hutool.json.JSONObject;
import com.xiaowen.wxpaytool.RequestBuilder;
import com.xiaowen.wxpaytool.WxpayV3Tool;
import com.xiaowen.wxpaytool.modle.PrepayRequest;
import com.xiaowen.wxpaytool.modle.PaybodyInfo;
import com.xiaowen.wxpaytool.modle.RefundRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SpringBootApplication
@RestController
public class APP {
    @Autowired
    WxpayV3Tool wxpayV3Tool; //工具类大房
    @Autowired
    RequestBuilder requestBuilder;//工具类二房

    public static void main(String[] args) {
        SpringApplication.run(APP.class,args);
    }
    //获得发起支付请求的参数
    @RequestMapping("/getprepayinfo")
    public PaybodyInfo getprepayinfo()  {
        PaybodyInfo getpaybodyinfo = null;
        try {
            PrepayRequest prepayRequest=requestBuilder.getprepayrequest("xiaowentest03","o4Mh-5ZqsTnF_0dnRXxBjMZ7Czgo","https://weixin.qq.com/",1);
            String prid = wxpayV3Tool.getprepayinfo(prepayRequest);//prid方便其他逻辑
            getpaybodyinfo = wxpayV3Tool.getpaybodyinfo(prid);//小程序发起支付请求的参数
        } catch (Exception e) {
            e.printStackTrace();
        }

        return getpaybodyinfo;
    }
    //根据业务系统订单号查询微信支付单详情
    @RequestMapping("/selectPayByOutTradeNo")
    public JSONObject selectPayByOutTradeNo()  {
        JSONObject jsonObject=null;
        try {
          jsonObject = wxpayV3Tool.selectPayByOutTradeNo("xiaowentest03");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    //根据微信支付单号查询微信支付单详情
    @RequestMapping("/selectPayByTransactionId")
    public JSONObject selectPayByTransactionId()  {
        JSONObject jsonObject=null;
        try {
            jsonObject = wxpayV3Tool.selectPayByTransactionId("4200001177202107252663971AAAA");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }
    //根据业务系统退单号查询微信退款单详情
    @RequestMapping("/findout2")
    public JSONObject test4()  {
        JSONObject jsonObject=null;
        try {
            jsonObject = wxpayV3Tool.selectRefundByOutTradeNo("tuixiaowentest02");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }
    //根据业务系统订单号关闭微信支付单
    @RequestMapping("/close")
    public JSONObject test5()  {
        JSONObject jsonObject=null;
        try {

            wxpayV3Tool.closePay("xiaowentest03");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }
    //根据业务系统的订单和退款单，发起退款申请
    @RequestMapping("/refundOrder")
    public JSONObject refundOrder(){
        JSONObject jsonObject=null;
        RefundRequest getprefundrequest = requestBuilder.getprefundrequest("xiaowentest02", "tuixiaowentest02", 1,"https://weixin.qq.com/");
        try {
            jsonObject = wxpayV3Tool.refundOrder(getprefundrequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    //查询交易单
    @RequestMapping("/selectTradebill")
    public JSONObject selectTradebill()  {
        JSONObject jsonObject=null;
        try {
            jsonObject=wxpayV3Tool.selectTradebill("2021-07-26");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }
    //查询资金单
    @RequestMapping("/selectFundflowBill")
    public JSONObject selectFundflowBill()  {
        JSONObject jsonObject=null;
        try {
            jsonObject=wxpayV3Tool.selectFundflowBill("2021-07-26");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    //回调处理，自动响应
    @RequestMapping("/notify")
    public JSONObject getnotify(HttpServletRequest request, HttpServletResponse response )  {
        JSONObject jsonObject=null;
        try {
            jsonObject=wxpayV3Tool.notify(request,response);
            //业务系统其他处理逻辑
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }



}
